import { goTo } from '@/modules/move'

/**
 * 获取 ops
 */
export const useGetOps = (room: MyRoom, pc: PowerCreep, amount?: number) => {
    if (!amount) {
        amount = Math.floor(pc.store.getFreeCapacity() / 2)
    }
    if (pc.store['ops'] >= amount || pc.store.getFreeCapacity() <= 0) return

    let target: AnyStoreStructure | null = null
    if (room.storage && room.storage.store['ops'] > 0) {
        target = room.storage
    } else if (room.terminal && room.terminal.store['ops'] > 0) {
        target = room.terminal
    } else {
        return
    }

    if (!pc.pos.isNearTo(target)) {
        goTo(pc, target.pos)
        return
    }

    pc.withdraw(target, 'ops', Math.min(amount, target.store['ops'], pc.store.getFreeCapacity()))
}
